<?php

use App\Modules\Device\src\GatewayWorker;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

/**
 * @定义的公共函数
 */

function indexs()
{
    return 1;
    /*$list =  DB::table("config")->select('name','value')->get();
    return $list;*/
}

//系统设置：配置信息
function configs($where = ''){
    $map = [];
    if(!empty($where)){
        $map['type'] = $where;
    }
    $configs = DB::table("config")->where($map)->select('name','value')->get();
    $config = array();
    foreach ($configs as $key => $value) {
        $config[$value->name] = $value->value;
    }
    return $config;
}
function menusurl(){
    $action = \Route::current()->getActionName();
    list($class, $method) = explode('@', $action);
    $class = substr(strrchr($class,'\\'),1);
    $class = explode('Controller',$class);
    $class = strtolower($class[0]);
    $method = strtolower($method);
    $rule = DB::table('auth_rule')->select('id')->where('href',$class.'/'.$method)->first();
    $rule = json_decode(json_encode($rule),true);
    return $rule['id'];
}

function menusaution(){
    $action = \Route::current()->getActionName();
    list($class, $method) = explode('@', $action);
    $class = substr(strrchr($class,'\\'),1);
    $class = explode('Controller',$class);
    return $class[0];
}

//无限极分类
function getTree($array, $pid=0,$lvl=0)
{
    $tree = [];
    foreach($array as $k => $v)
    {
        if($v['pid'] == $pid)
        { //父亲找到儿子
            $v['lvl']=$lvl + 1;
            $v['children'] = getTree($array, $v['id'],$lvl+1);
            $tree[] = $v;
        }
    }
    return $tree;
}

function ssh5($command)
{
    $path        = base_path();
    $command_str = 'cd '.$path.' && php artisan workman '.$command;
    $msg         = shell_exec($command_str);
    switch ($command)
    {
        case 'start --d':
            return ['code'=> 200,'msg'=>'GatewayWorker 服务开启：' .$msg];
            break;
        case 'stop':
            return ['code'=> 2001,'msg'=>'GatewayWorker 服务停止：' .$msg];
            break;
        case 'reload':
            return ['code'=> 2002,'msg'=>'GatewayWorker 服务重启中...：' .$msg];
            break;
        default:
            if (strpos($msg,'not run')) {
                return ['code'=> 2001,'msg'=>'GatewayWorker 服务状态：' .$msg]; // 停止
            } else{
                return ['code'=> 200,'msg'=>'GatewayWorker 服务状态：' .$msg]; // 运行中
            }
    }
}

//workerman状态
function ssh($command){
    try{
        //在php.ini shell_exec禁用函数删除
        $cmd = 'php '.base_path().'/artisan workman '.$command;
        $msg = shell_exec($cmd);
        switch ($command)
        {
            case 'start --d':
                return ['code'=> 200,'msg'=>'GatewayWorker 服务开启：' .$msg];
                break;
            case 'stop':
                return ['code'=> 2001,'msg'=>'GatewayWorker 服务停止：' .$msg];
                break;
            case 'reload':
                return ['code'=> 2002,'msg'=>'GatewayWorker 服务重启中...：' .$msg];
                break;
            default:
                if (strpos($msg,'not run')) {
                    return ['code'=> 2001,'msg'=>'GatewayWorker 服务状态：' .$msg]; // 停止
                } else{
                    return ['code'=> 200,'msg'=>'GatewayWorker 服务状态：' .$msg]; // 运行中
                }
        }
    }catch ( Exception $e){
        return ['code' => 4009, 'msg' => '出现异常,连接失败'];
    }
}
